package wordsDistribution;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class TFIDFKeywordGenerator {

	public static void main(String[] args) throws IOException {
		
		String type = "training";
		
		String output = "/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/tfidf/"+type+"/";
		
		String distribution = "/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/WordsDistribution/"+type+"/";
		
		WordsDistributionLoader wdl = new WordsDistributionLoader();
		
		wdl.load(distribution);

		Map<String,Double> idf = new HashMap<String, Double>();
		
		Set<Integer> files = wdl.getFiles();
		
		double size = (double)files.size();
		
		for (String word : wdl.getWords()) {
			
			idf.put(word, Math.log(((double)size)/((double)wdl.getDocumentFrequency(word))));
			
		}
			
		Map<Integer,Map<String,Double>> t = new HashMap<Integer, Map<String,Double>>();
		
		for (Integer index : files) {
			
			Map<String,Double> tt = new HashMap<String, Double>();
			
			for (Entry<String, Long> entry : wdl.getWordMap(index).entrySet()) {
				
				double tf = (double)entry.getValue();
				
				double tfIdf = idf.get(entry.getKey())*tf;
				
				tt.put(entry.getKey(), tfIdf);
				
			}
			
			t.put(index, tt);
		}
		
		for (Entry<Integer,Map<String,Double>> toPrint : t.entrySet()) {
			
			write(new File(output + toPrint.getKey()),toPrint.getValue());
			
		}
		
	}

	private static void write(File file, Map<String, Double> map) throws IOException {
		
		List<String> words = new ArrayList<String>(map.keySet());
		
		Comparator<String> c = new HashBasedComparator(map);
		
		Collections.sort(words, c);
		
		BufferedWriter bw = new BufferedWriter(new FileWriter(file));
		
		for (String word : words) {
			
			bw.write(word + WordsDistribution.SEPARATOR + map.get(word));
			
			bw.newLine();
			
		}
		
		bw.close();
	}
	
}
